package com.sec.android.app.myfiles.module.local.file;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.ToSelectModeCmd;
import com.sec.android.app.myfiles.feature.LockUnlockMgr;
import com.sec.android.app.myfiles.feature.PrivateModeMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.operation.FileOperator;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.StorageMonitor;

import java.util.ArrayList;

public class LocalFileMenuImp extends AbsMenuImp {
    public LocalFileMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);

        String curPath = mNavigationInfo.getCurRecord().getFullPath();
        if (StorageMonitor.startWithUsbRoot(curPath)) {
            setMenuItemVisibility(menu, R.id.menu_import, NavigationManager.getInstance(mFragment.getProcessId()).isFromPrivateBox());
        }

		setMenuItemVisibility(menu, R.id.menu_prepare_move_private_folder, true);
        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_create_folder, !(FileOperator.isWorking() || isRemovedExtSDCard()));
        setMenuItemVisibility(menu, R.id.menu_search, (!(mNavigationInfo.isUsbStorage() || isRemovedExtSDCard() || FileUtils.isEmptyStorage(mNavigationInfo, mFragment))));
        setMenuItemVisibility(menu, R.id.menu_toggle_list_type, !isRemovedExtSDCard() && mListItemCount > 0);
        setMenuItemEnabled(menu, R.id.menu_toggle_list_type, !isRemovedExtSDCard() && mListItemCount > 0);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, AppFeatures.isTabletUIMode() && NavigationManager.isPossibleToActionMode() && ShortcutUtils.getShortcutItemsCount(mContext) > 0);
    }

    public boolean _onOptionsItemSelected(MenuItem item) {
        boolean bRet = super._onOptionsItemSelected(item);

        if (!bRet) {
            bRet = true;
            int id = item.getItemId();
            int processId = mFragment.getProcessId();
            switch (id) {
                case R.id.menu_zip:
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.ZIP, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                    MyFilesFacade.compress(processId, mContext, mFragment, mFragment.getSelectedFile());
                    break;
                case R.id.menu_unzip:
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.UNZIP, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                    MyFilesFacade.decompress(processId, mContext, mFragment, mFragment.getSelectedFile());
                    break;
                case R.id.menu_unzip_current_folder:
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.UNZIP_TO_CURRENT_FOLDER, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                    MyFilesFacade.decompressToCurrentFolder(processId, mContext, mFragment, mFragment.getSelectedFile());
                    break;
                default:
                    bRet = false;
                    break;
            }
        }
        return bRet;
    }

    public void onPrepareEditOptionsMenu(Menu menu) {
        super.onPrepareEditOptionsMenu(menu);

        ArrayList<FileRecord> selectedItem = mFragment.getSelectedFile();
        int selectedCount = selectedItem != null ? selectedItem.size() : 0;
        String curPath = mNavigationInfo.getCurRecord().getFullPath();

        if (selectedCount > 0) {
            setMenuItemVisibility(menu, R.id.menu_zip, true);

            if (isSharableRecords(selectedItem)) {
                setMenuItemVisibility(menu, R.id.menu_share, true);
            }

            if (AppFeatures.isKnoxDesktopMode() && mFragment.getActionModeType() == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION) {
                showMoreMenu(menu, R.id.menu_move, false);
            }

            PrivateModeMgr privateModeMgr = PrivateModeMgr.getInstance(mContext);
            if (privateModeMgr.isReady()) {
                if (privateModeMgr.isPrivateModePath(curPath)) {
                    if (NavigationManager.getInstance(mFragment.getProcessId()).isFromPrivateBox()) {
                        setMenuItemVisibility(menu, R.id.menu_export, true);
                    } else {
                        setMenuItemVisibility(menu, R.id.menu_remove_from_private, true);
                    }
                } else {
                    setMenuItemVisibility(menu, R.id.menu_move_to_private, true);
                }
            }

            showKnoxMenu(menu, false, PrivatePath.ONLY_NON_PRIVATE);

            if (StorageMonitor.startWithUsbRoot(curPath)) {
                setMenuItemVisibility(menu, R.id.menu_move_to_private, false);
            }

            if (selectedCount == 1) {
                FileRecord item = selectedItem.get(0);

                if (item != null) {
                    if (!item.isDirectory()) {
                        if (LockUnlockMgr.isSupported()) {
                            if (LockUnlockMgr.getInstance().isEncryptionFile(item.getFullPath())) {
                                setMenuItemVisibility(menu, R.id.menu_unlock, true);
                                setMenuItemVisibility(menu, R.id.menu_change_pin, true);
                            }
                        }

                        if (item.getFileType() == FileType.ZIP) {
                            setMenuItemVisibility(menu, R.id.menu_zip, false);
                            setMenuItemVisibility(menu, R.id.menu_unzip, true);
                            setMenuItemVisibility(menu, R.id.menu_unzip_current_folder, true);
                        }
                    }

                    if (ShortcutUtils.isSupportShortcut(mContext, item)) {
                        setMenuItemVisibility(menu, R.id.menu_add_shortcut, true);
                    }

                }
            }
        }

        setMenuItemVisibility(menu, R.id.menu_operator_move_private_folder, true);
    }

    private boolean isRemovedExtSDCard() {
        boolean bRet = false;
        FileRecord record = mNavigationInfo.getCurRecord();
        if (StorageMonitor.isRemovedExtSDCard(record.getFullPath())) {
            bRet = true;
        }

        return bRet;
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        if (!mNavigationInfo.isEmptySdCardInfo()) {
            super.createContextualMenuOnBackground(menu);
            menu.removeItem(R.id.menu_share);
        }
    }

    @Override
    protected void createContextualMenuOnLeftPanel(ContextMenu menu, FileRecord record) {
        // Do nothing. It will be processed in FolderTreeMenuImp
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        if (fragment != null) {
            AbsMyFilesFragment.ActionModeType actionModeType = fragment.getActionModeType();
            if (fragment.getTouchPosition() == BACKGROUND) {
                return super._onContextualItemSelected(item, null, fragment.getNavigationInfo().getCurRecord(), fragment);
            } else {
                if (actionModeType == AbsMyFilesFragment.ActionModeType.NORMAL) {
                    srcRecordList = new ArrayList<>();
                    srcRecordList.add(record);
                    return super._onContextualItemSelected(item, srcRecordList, record, fragment);
                } else if (actionModeType == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION || actionModeType == AbsMyFilesFragment.ActionModeType.SELECT_FILE_FOLDER_OPERATION) {
                    srcRecordList = fragment.getSelectedFile();
                    if (srcRecordList != null) {
                        FileRecord recordList = srcRecordList.get(0);
                        return super._onContextualItemSelected(item, srcRecordList, recordList, fragment);
                    }
                }
            }
        }
        return false;
    }

    @Override
    protected boolean homeAsUpEnabled() {
        if (mNavigationInfo != null && !mNavigationInfo.isLeftPanelDisabled() && !PreferenceUtils.getShowRecentFiles(mContext)) {
            return false;
        }
        return super.homeAsUpEnabled();
    }

    @Override
    public String getActionBarTitle() {
        if (AppFeatures.isTabletUIMode() || (mNavigationInfo != null && !mNavigationInfo.isLeftPanelDisabled() && !PreferenceUtils.getShowRecentFiles(mContext))) {
            return mContext.getResources().getString(R.string.app_name);
        }
        return null;
    }
}
